﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.CommonDao;
using CSP.Dao.IObjectDaos.CSP;
using CSP.DataAccess;
using CSP.Model.CSP;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class OrderDao : CSPCommonDao, IOrderDao
    {
        public OrderDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }

        public ICollection<Model.CSP.Order> FindAll()
        {
            return _context.Orders.ToArray();
        }

        public Order GetDataByOrderNumber(string orderNumber)
        {
            return _context.Orders.FirstOrDefault(o => o.OrderNumber == orderNumber);
        }

        /// <summary>
        /// 获取客户的批量订单
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public ICollection<Order> FindByOrderUser(string userName, DateTime startTime, DateTime endTime, int pageIndex, int pageSize, out int pageCount)
        {
            pageCount = _context.Orders.Where(o => o.OrderUserName == userName && o.OrderTime >= startTime && o.OrderTime >= endTime).Count();

            ICollection<Order> result = null;
            var orders = _context.Orders.Where(o => o.OrderUserName == userName && o.OrderTime >= startTime && o.OrderTime >= endTime).Skip(pageIndex * pageSize).Take(pageSize);
            if (orders.Count() != 0)
            {
                result = orders.ToArray();
            }
            return result;
        }

        /// <summary>
        /// 获取服务者的批量订单
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public ICollection<Order> FindByServiceUser(string userName, DateTime startTime, DateTime endTime, int pageIndex, int pageSize, out int pageCount)
        {
            pageCount = _context.Orders.Where(o => o.ServiceUserName == userName && o.OrderTime >= startTime && o.OrderTime >= endTime).Count();

            ICollection<Order> result = null;
            var orders = _context.Orders.Where(o => o.ServiceUserName == userName && o.OrderTime >= startTime && o.OrderTime >= endTime).Skip(pageIndex * pageSize).Take(pageSize);
            if (orders.Count() != 0)
            {
                result = orders.ToArray();
            }
            return result;
        }

        public ICollection<Model.CSP.Order> GeDatatByIds(string[] orderNumbers)
        {
            return _context.Orders.Where(o => orderNumbers.Contains(o.OrderNumber)).ToArray();
        }

        public void Add(Model.CSP.Order obj)
        {
            _context.Orders.Add(obj);
            _context.SaveChanges();
        }

        public void Update(Model.CSP.Order obj)
        {
            Order order = GetDataByOrderNumber(obj.OrderNumber);
            if (order != null)
            {
                order = obj;
            }
            _context.SaveChanges();
        }

        public Order DeleteById(string orderNumber)
        {
            Order order = GetDataByOrderNumber(orderNumber);
            _context.Orders.Remove(order);

            _context.SaveChanges();
            return order;
        }



        public int FindCountByServiceInfoId(int serviceInfoId)
        {
            return _context.Orders.Count(o => o.ServiceInfoId == serviceInfoId);
        }


        public List<string> FindOrderNumbersByConsumeUser(string consumeUserName)
        {
            List<string> result = new List<string>();
            var orderNumbers = _context.Orders.Where(o => o.OrderUserName == consumeUserName).Select(o => o.OrderNumber);
            if (orderNumbers.Count() != 0)
            {
                result = orderNumbers.ToList();
            }
            return result;
        }

        public List<string> FindOrderNumbersByServiceUser(string serviceUserName)
        {
            List<string> result = new List<string>();
            var ids = _context.Orders.Where(o => o.ServiceUserName == serviceUserName).Select(o => o.OrderNumber);
            if (ids.Count() != 0)
            {
                result = ids.ToList();
            }
            return result;
        }

        public List<string> FindOrderNumbersByConsumeUserNameAndStartTimeAndEndTime(string consumeUserName, DateTime startTime, DateTime endTime)
        {
            List<string> result = new List<string>();
            var ids = _context.Orders.Where(o => o.OrderUserName == consumeUserName && o.OrderTime >= startTime && o.OrderTime <= endTime).Select(o => o.OrderNumber);
            if (ids.Count() != 0)
            {
                result = ids.ToList();
            }
            return result;
        }

        public List<string> FindOrderNumbersByServiceUserNameAndStartTimeAndEndTime(string serviceUserName, DateTime startTime, DateTime endTime)
        {
            List<string> result = new List<string>();
            var orderNumbers = _context.Orders.Where(o => o.ServiceUserName == serviceUserName && o.OrderTime >= startTime && o.OrderTime <= endTime).Select(o => o.OrderNumber);
            if (orderNumbers.Count() != 0)
            {
                result = orderNumbers.ToList();
            }
            return result;
        }

        public Order GetByOrderNumberAndConsumeUser(string orderNumber, string consumeUserName)
        {
            return _context.Orders.FirstOrDefault(o => o.OrderUserName == consumeUserName && o.OrderNumber == orderNumber);
        }

        public Order GetByIdAndServiceUser(string orderNumber, string serviceUserName)
        {
            return _context.Orders.FirstOrDefault(o => o.OrderUserName == serviceUserName && o.OrderNumber == orderNumber);
        }

        public List<string> FindOrderNumbersByConsumeUserAndStatus(string consumeUser, Model.CSPEnum.OrderStatus status)
        {
            List<string> result = new List<string>();
            var orderNumbers = _context.Orders.Where(o => o.OrderUserName == consumeUser && o.status == (int)status).Select(o => o.OrderNumber);
            if (orderNumbers.Count() != 0)
            {
                result = orderNumbers.ToList();
            }
            return result;
        }

        public List<string> FindOrderNumbersByServiceUserAndStatus(string serviceUser, Model.CSPEnum.OrderStatus status)
        {
            List<string> result = new List<string>();
            var orderNumbers = _context.Orders.Where(o => o.ServiceUserName == serviceUser && o.status == (int)status).Select(o => o.OrderNumber);
            if (orderNumbers.Count() != 0)
            {
                result = orderNumbers.ToList();
            }
            return result;
        }

        public ICollection<Order> GetByOrderNumbers(List<string> orderNumbers, int pageIndex, int pageSize)
        {
            List<Order> result = new List<Order>();
            var orders = _context.Orders.Where(o => orderNumbers.Contains(o.OrderNumber)).OrderByDescending(o => o.OrderNumber).Skip(pageIndex * pageSize).Take(pageSize);
            if (orders.Count() != 0)
            {
                result = orders.ToList();
            }
            return result;
        }

        //public Order UpdateServiceStatus(int id, string serviceUserName, Model.CSPEnum.ServiceInfoStatus serviceStatus)
        //{
        //    Order order = _context.Orders.
        //}

        //public Order UpdateConsumeStatus(int id, string consumeUserName, Model.CSPEnum.ServiceInfoStatus serviceStatus)
        //{
        //    throw new NotImplementedException();
        //}


        /// <summary>
        /// 根据提供的(订单编号 或 输入合作标题 或者合作者用户名)和消费用户获取数据ids
        /// </summary>
        /// <param name="searchValue"></param>
        /// <returns></returns>
        public List<string> FindByConsumeUserAndSearchValue(string consumeUser, string searchValue)
        {
            List<string> result = new List<string>();
            var orderNumbers = _context.Orders.Where(o => o.OrderUserName == consumeUser && (o.OrderNumber == searchValue || o.ServiceTitle.Contains(searchValue) || o.ServiceUserName == searchValue)).OrderByDescending(o => o.OrderNumber).Select(o => o.OrderNumber);
            if (orderNumbers.Count() != 0)
            {
                result = orderNumbers.ToList();
            }
            return result;
        }

        /// <summary>
        /// 根据提供的(订单编号 或 输入合作标题 或者合作者用户名)和服务用户获取数据Ids
        /// </summary>
        /// <param name="searchValue"></param>
        /// <returns></returns>
        public List<string> FindByServiceUserAndSearchValue(string serviceUser, string searchValue)
        {
            List<string> result = new List<string>();
            var orderNumbers = _context.Orders.Where(o => o.ServiceUserName == serviceUser && (o.OrderNumber == searchValue || o.ServiceTitle.Contains(searchValue) || o.OrderUserName == searchValue)).OrderByDescending(o => o.OrderNumber).Select(o => o.OrderNumber);
            if (orderNumbers.Count() != 0)
            {
                result = orderNumbers.ToList();
            }
            return result;
        }

        [Obsolete("此方法废弃不可用", true)]
        public Order GetDataById(int id)
        {
            throw new NotImplementedException();
        }

        [Obsolete("此方法废弃不可用", true)]
        public ICollection<Order> GeDatatByIds(int[] Ids)
        {
            throw new NotImplementedException();
        }

        [Obsolete("此方法废弃不可用", true)]
        public Order DeleteById(int id)
        {
            throw new NotImplementedException();
        }

        public List<string> FindOrderNumbersByServiceInfoId(int serviceInfoId)
        {
            List<string> result = new List<string>();
            var orderNumbers = _context.Orders.Where(o => o.ServiceInfoId == serviceInfoId).Select(o => o.OrderNumber);
            if (orderNumbers.Count() != 0)
            {
                result = orderNumbers.ToList();
            }
            return result;
        }
    }
}
